package org.tmsps.ne4spring.utils.number;

import java.math.BigDecimal;
import java.util.regex.Pattern;

public class NumUtil {

	public static BigDecimal getBD(String value) {
		if (value == null || "".equals(value.trim())) {
			return NumUtil.getBD("0");
		}
		value = value.trim();
//		if (!checkDecimals(value)) {
//			return new BigDecimal("0");
//		}
		try {
			return new BigDecimal(value);
		} catch (Exception e) {
			e.printStackTrace();
			return NumUtil.getBD("0");
		}
	}

	public static boolean checkDecimals(String decimals) {
		String regex = "\\-?[1-9]\\d+(\\.\\d+)?";
		return Pattern.matches(regex, decimals);
	}

	public static void main(String[] args) {
		String srate = "8";
		String base = "4919";
		System.err.println(NumUtil.getBD(srate));
		BigDecimal bd = NumUtil.getBD(srate).multiply(NumUtil.getBD(base)).divide(NumUtil.getBD("100"));
		System.err.println(bd);
		System.err.println(checkDecimals(base));

	}

	public static BigDecimal add(String a, String b) {
		return NumUtil.getBD(a).add(NumUtil.getBD(b));
	}
}
